package com.qingcheng.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.wxpay.sdk.Config;
import com.github.wxpay.sdk.WXPayRequest;
import com.github.wxpay.sdk.WXPayUtil;
import com.qingcheng.service.order.OrderService;
import com.qingcheng.service.order.WxPayService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: LDeng
 * @Date: 2020-07-07 16:30
 */
@Service
public class WxPayServiceImpl implements WxPayService {

    @Autowired
    private Config config;


    //关闭订单操作

    @Override
    public Map closePay(String outtradeno) {
        Map map=new HashMap();

            return map;

    }

    @Override
    public Map createNative(String orderId, Integer money, String notifyUrl,String... attach) {
        //1 封装请求参数
        try {
        Map<String,String> map=new HashMap();
        map.put("appid",config.getAppID());//公众账号id
        map.put("mch_id",config.getMchID());//商户号
        map.put("nonce_str",WXPayUtil.generateNonceStr());//随机字符串
        //签名
        map.put("body","青橙");//商品描述
        map.put("out_trade_no",orderId);//订单号
        map.put("total_fee",money+"");//金额 单位为分
        map.put("spbill_create_ip","127.0.0.1");//终端IP
        map.put("notify_url",notifyUrl);//回调地址
        map.put("trade_type","NATIVE");//交易类型
            if (attach != null && attach.length > 0) {
                map.put("attach",attach[0]);
            }


        String xmlParam = WXPayUtil.generateSignedXml(map, config.getKey());//xml格式的参数
       // 2 发送请求
//        WXPayRequest wxPayRequest=new WXPayRequest(config);
//        String xmlResult = wxPayRequest.requestWithCert("/pay/unifiedorder", null, xmlParam, false);
//        System.out.println("结果"+xmlResult);

        //3, 返回结果
         //   Map<String,String> mapResult=WXPayUtil.xmlToMap(xmlResult);
            Map m=new HashMap();
            String codeurl1="weixin://wxpay/gizpayurl?pr=K4FTBvd";
            m.put("code_url",codeurl1);
            m.put("total_fee","99");
            m.put("out_trade_no","1281100028882063360");
            return m;
        } catch (Exception e) {
            e.printStackTrace();
            return new HashMap();
        }
    }


    @Autowired
    private OrderService orderService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public void notifyLogic(String xml) {
        //对xml解析， 解析成map
        try {
            Map<String, String> map = WXPayUtil.xmlToMap(xml);
            //验证签名
            boolean signatureValid = WXPayUtil.isSignatureValid(map, config.getKey());
            System.out.println("验证签名是否正确========>"+signatureValid);
            System.out.println(map.get("out_trade_no"));
            System.out.println(map.get("result_code"));

            //发送订单号给RabbitMQ
                rabbitTemplate.convertAndSend("paynotify","",map.get("out_trade_no"));
            System.out.println("<==================debug===================>");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
